package com.wzdigit.wms.wms.service.core;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.order.TrsnDtl;
import com.wzdigit.wms.basic.domain.order.TrsnDtlGoods;
import com.wzdigit.wms.basic.domain.order.TrsnHeader;
import com.wzdigit.wms.basic.mapper.core.order.TrsnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnHeaderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class TrsnManagerService {

    @Autowired
    private TrsnHeaderMapper trsnHeaderMapper;

    @Autowired
    private TrsnDtlMapper trsnDtlMapper;

    @Autowired
    private TrsnDtlGoodsMapper trsnDtlGoodsMapper;

    public TrsnHeaderMapper getTrsnHeaderMapper() {
        return trsnHeaderMapper;
    }

    public TrsnDtlMapper getTrsnDtlMapper() {
        return trsnDtlMapper;
    }

    public TrsnDtlGoodsMapper getTrsnDtlGoodsMapper() {
        return trsnDtlGoodsMapper;
    }

    public TrsnHeader selectByFmDoc(String billNo) {
        QueryWrapper<TrsnHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("FM_DOC", billNo);
        List<TrsnHeader> list = this.trsnHeaderMapper.selectList(queryWrapper);
        if (list.size() == 0)
            return null;
        return list.get(0);
    }

    public List<TrsnDtl> selectDetailsByTrsnId(Integer id) {
        QueryWrapper<TrsnDtl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRSN_HEADER_ID", id);
        return this.trsnDtlMapper.selectList(queryWrapper);
    }

    /**
     * 新增 一行 的单据信息
     *
     * @param trsnHeader
     * @param trsnDtl
     * @param dtlGoodsList
     */
    public void insertTrsnInfo(TrsnHeader trsnHeader, TrsnDtl trsnDtl, List<TrsnDtlGoods> dtlGoodsList) {
        if (trsnHeader.getTrsnHeaderId() == null) {
            trsnHeader.setCreateTime(new Date());
            this.trsnHeaderMapper.insert(trsnHeader);
        }
        trsnDtl.setNoteNo(trsnHeader.getNoteNo());
        trsnDtl.setTrsnHeaderId(trsnHeader.getTrsnHeaderId());
        trsnDtl.setCreateTime(new Date());
        this.trsnDtlMapper.insert(trsnDtl);

        for (TrsnDtlGoods item : dtlGoodsList) {
            item.setCreateTime(new Date());
            item.setTrsnDtlId(trsnDtl.getTrsnDtlId());
            this.trsnDtlGoodsMapper.insert(item);
        }
    }

    /**
     * 新增 一行 的单据信息
     *
     * @param trsnHeader
     * @param trsnDtl
     * @param dtlGoodsList
     */
    public void insertTrsnInfo(TrsnHeader trsnHeader, List<TrsnDtl> trsnDtls) {
        if (trsnHeader.getTrsnHeaderId() == null) {
            trsnHeader.setCreateTime(new Date());
            this.trsnHeaderMapper.insert(trsnHeader);
        }
        for (TrsnDtl item : trsnDtls) {
            item.setNoteNo(trsnHeader.getNoteNo());
            item.setTrsnHeaderId(trsnHeader.getTrsnHeaderId());
            item.setCreateTime(new Date());
            this.trsnDtlMapper.insert(item);
        }
    }
}
